﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.Web.ASPxCallback;
using System.Web.Security;
using System.Configuration;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxGridView;

namespace TireService.Web.View.WarehouseOperatorView
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
            {
                TireServiceDB db = new TireServiceDB();

                ddlContainer.DataSource = db.GetContainers();
                ddlContainer.ValueField = "ContainerId";
                ddlContainer.TextField = "ContainerNumber";
                ddlContainer.DataBind();
            }

            LoadStockTask(Convert.ToInt32(ddlContainer.Value));
            LoadTireSetWaiting(Convert.ToInt32(ddlContainer.Value));
        }

        /// <summary>
        /// Function that updates
        /// </summary>
        protected void ddlScheduleDate_Callback(object source, CallbackEventArgsBase e)
        {
            // Initiate datalayer
            TireServiceDB db = new TireServiceDB();

            BindScheduleDate(db, Convert.ToInt32(e.Parameter));
        }

        public void BindScheduleDate(TireServiceDB db, int ContainerId)
        {
            // Get service cars for container
            int[] servicecars = db.GetContainerServiceCars(ContainerId);

            var schedules = from p in db.ServiceCarSchedules
                            where p.ScheduleDate > DateTime.Now.AddMonths(-5) & servicecars.Contains(p.ServiceCarId)
                            select new
                            {
                                ScheduleDate = p.ScheduleDate
                            };

            // TODO: Change date, to now when releasing
            ddlScheduleDate.DataSource = schedules.Distinct().ToList();; //db.GetServiceCarSchedules(servicecars, DateTime.Now.AddMonths(-5));
            ddlScheduleDate.ValueField = "ScheduleDate";
            ddlScheduleDate.TextField = "ScheduleDate";
            ddlScheduleDate.DataBind();
        }

        /// <summary>
        /// Postback function that updates overview of tasks
        /// </summary>
        protected void gvStockTask_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            //// Get value (ContainerId)
            int ContainerId = Convert.ToInt32(e.Parameters);

            LoadStockTask(ContainerId);
        }

        public void LoadStockTask(int ContainerId)
        {
            TireServiceDB db = new TireServiceDB();
            gvStockTask.DataSource = db.GetTasksExtendedView().Where(p => p.ContainerId == ContainerId & p.TaskTypeId == (int)Shared.TaskType.StockTask & p.TaskTypeStatusId == (int)Shared.TaskTypeStatus.StockTask_Created).ToList();
            gvStockTask.DataBind();

            //gvTaskBackLog.GroupBy(gvTaskBackLog.Columns["ScheduleDate"]);
            //gvTaskBackLog.ExpandAll();
        }

        /// <summary>
        /// Gridview detail function that updates / show details for tasks
        /// </summary>
        protected void gvTaskDetail_DataSelect(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();
            ASPxGridView gvTaskDetail = (sender as ASPxGridView);
            gvTaskDetail.DataSource = db.GetTaskDetailsExtendedView(Convert.ToInt32((sender as ASPxGridView).GetMasterRowKeyValue()));
        }

        /// <summary>
        /// Postback function that updates overview of waiting tireset
        /// </summary>
        protected void gvTireSetWaiting_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            // Get value (ContainerId)
            int ContainerId = Convert.ToInt32(e.Parameters);

            LoadTireSetWaiting(ContainerId);
        }

        public void LoadTireSetWaiting(int ContainerId)
        {
            TireServiceDB db = new TireServiceDB();
            gvTireSetWaiting.DataSource = db.GetCarTireSetViews().Where(p => p.ContainerId == ContainerId & p.TireSetStatusId == (int)Shared.TireSetStatus.AwaitsMoveToStock).ToList();
            gvTireSetWaiting.DataBind();

            //gvTireSetWaiting.GroupBy(gvTaskBackLog.Columns["ScheduleDate"]);
            //gvTireSetWaiting.ExpandAll();
        }

        /// <summary>
        /// Postback function that updates overview of tireset that wait for being moved to another container/warehouse
        /// </summary>
        protected void gvStockMovingTask_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            //// Get value (ServiceCarId)
            //int ServiceCarId = Convert.ToInt32(e.Parameters);

            //LoadDayPlanBackLog(ServiceCarId);
        }
    }
}